import tushare as ts
import time
import pandas as pd
import multiprocessing

pro = ts.pro_api('8b334e2d5d482292aa8b516c7f59dd6d23c0266dac3dc407dfb63ea1')


def tedliu(ts_code):
    df = pro.income(ts_code=ts_code, start_date='20200901', end_date='20201130',
                    fields='ts_code,end_date,total_revenue,total_profit')
    return df[:1]


if __name__ == "__main__":
    print("...Starting...")
    now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print(now)
    now1 = time.time()
    # pool = multiprocessing.Pool(processes=1)
    # multiple_results = [pool.apply_async(tedliu, args=(i,)) for i in data['ts_code']]
    # lst = [res.get() for res in multiple_results]
    # pool.close()
    # pool.join()

    data = pro.stock_basic(exchange='', list_status='L', fields='ts_code')
    for _ in data.ts_code:
        lst.append(tedliu(_))
        time.sleep(1)
    result = pd.concat(lst)
    result.to_excel('股票财务.xlsx')
    
    print("------------------------------------")
    print("...Finished...")
    now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print(now)
    now2 = time.time()
    now3 = now2 - now1
    print("...用时{} Secs...".format(round(now3, 2)))